package com.lemon.smartsafetynew.ui.activity.contact;

import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;

import com.lemon.android.http.LemonRequestParams;
import com.lemon.smartsafety.R;
import com.lemon.smartsafety.app.AppManager;
import com.lemon.smartsafety.app.AppService;
import com.lemon.smartsafety.app.Constants;
import com.lemon.smartsafety.app.IApplication;
import com.lemon.smartsafety.entity.event.http.HttpCallbackEvent;
import com.lemon.smartsafety.http.callback.HandlerCallback;
import com.lemon.smartsafety.listener.RecyclerTClickListener;
import com.lemon.smartsafety.listener.RecyclerTNewClickListener;
import com.lemon.smartsafetynew.ui.activity.base.SwipeBackActivity;
import com.lemon.smartsafety.utils.app.HUDUtil;
import com.lemon.smartsafetynew.entity.contact.ContactBo;
import com.lemon.smartsafetynew.entity.contact.PersonVo;
import com.lemon.smartsafetynew.entity.event.contact.PersonEvent;
import com.lemon.smartsafetynew.net.services.ContactServices;
import com.lemon.smartsafetynew.ui.adapter.contact.LabelListAdapter;
import com.lemon.smartsafetynew.ui.adapter.contact.PersonListAdapter;
import com.lemon.smartsafetynew.util.constants.KeyConstants;

import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import butterknife.Bind;

/**
 * 政府人员列表
 * Created by Andrew on 2017/10/31.
 */

public class GovGridLeaderListActivity extends SwipeBackActivity
        implements RecyclerTClickListener<PersonVo>, RecyclerTNewClickListener<PersonVo> {

    @Bind(R.id.recyclerView)
    RecyclerView recyclerView;
    @Bind(R.id.department_recyclerView)
    RecyclerView departmentRecyclerView;

    private static String tag = "";
    private static String company_id = "";

    private List<PersonVo> mDataList = new ArrayList<>();
    private List<PersonVo> mDepartmentLabels = new ArrayList<>();
    private LabelListAdapter mLabelAdapter;
    private PersonListAdapter mAdapter;

    private String parentId = "";

    @Override
    protected void getBundleArgs() {
        super.getBundleArgs();

        Bundle bundle = getIntent().getExtras();
        if (bundle != null) {
            if (bundle.containsKey(KeyConstants.KEY_TAG)) {
                tag = bundle.getString(KeyConstants.KEY_TAG);
            }
            if (bundle.containsKey(KeyConstants.KEY_COMPANY_ID)) {
                company_id = bundle.getString(KeyConstants.KEY_COMPANY_ID);
            }
        }
    }

    @Override
    protected int getContentViewId() {
        return R.layout.activity_gov_grid_lead_list;
    }

    @Override
    protected void initTitle() {
        this.setBarTitle(getString(R.string.contact_gov_grid_str));
    }

    @Override
    protected void callFinish() {
        AppManager.getAppManager().finishActivity(GovGridLeaderListActivity.this);
    }

    @Override
    protected void initViews() {
        super.initViews();

        /**
         * 部门栏
         */
        initDepartmentRecyclerView();
        /**
         * 人员列表
         */
        initRecyclerView();

        /**
         * 第一个部门
         */
        PersonVo personVo = new PersonVo();
        personVo.setUser_id("");
        personVo.setUser_name("人员列表");
        refreshDepartmentRecyclerView(personVo);

        /**
         * 获取人员和部门数据
         */
        getPersonList(parentId);

    }

    /**
     * 人员列表
     */
    private void initRecyclerView() {
        LinearLayoutManager layoutManager = new LinearLayoutManager(IApplication.context(), LinearLayoutManager.VERTICAL, false);
        recyclerView.setLayoutManager(layoutManager);
        mAdapter = new PersonListAdapter(mDataList, this);
        recyclerView.setAdapter(mAdapter);
    }

    /**
     * 部门栏
     */
    private void initDepartmentRecyclerView() {
        LinearLayoutManager layoutManager = new LinearLayoutManager(IApplication.context(), LinearLayoutManager.HORIZONTAL, false);
        departmentRecyclerView.setLayoutManager(layoutManager);
        mLabelAdapter = new LabelListAdapter(mDepartmentLabels, this);
        departmentRecyclerView.setAdapter(mLabelAdapter);
    }

    /**
     * 刷新部门栏
     */
    private void refreshDepartmentRecyclerView(PersonVo personVo) {
        mDepartmentLabels.add(personVo);
        mLabelAdapter.notifyDataSetChanged();
        departmentRecyclerView.scrollToPosition(mDepartmentLabels.size() - 1);
    }

    /**
     * 获取网路数据
     */
    public void getPersonList(String parentId) {
        LemonRequestParams params = new LemonRequestParams("");
        //params.put("company_id", company_id);
        params.put("parent_id", parentId);
        ContactServices
                .init()
                .getGovGridLeader(params);
    }

    @Override
    public void onItemClick(PersonVo data, int position) {

        if (ContactBo.Type.department.getValue().equals(data.getType())) {

            refreshDepartmentRecyclerView(data);
            getPersonList(data.getUser_id()); //部门的department_id就是user_id

        } else {

            PersonEvent personEvent = new PersonEvent();
            personEvent.setPersonVo(data);
            personEvent.setTag(tag);
            personEvent.setFlag(GovGridLeaderListActivity.class.getName());

            AppService.getBus().post(personEvent);
            callFinish();
        }

    }

    @Override
    public void onItemClickNew(PersonVo data, int position) {

        List<PersonVo> list = new ArrayList<>();

        for (Iterator<PersonVo> iter = mDepartmentLabels.iterator(); iter.hasNext(); ) {
            PersonVo personVo = iter.next();

            list.add(personVo);

            if (personVo.getUser_id().equals(data.getUser_id())) {
                break;
            }
        }

        if (mLabelAdapter != null) {
            mDepartmentLabels.clear();
            mLabelAdapter.notifyDataSetChanged();
            mLabelAdapter.addItems(list);
        }

        departmentRecyclerView.scrollToPosition(mDepartmentLabels.size() - 1);
        getPersonList(data.getUser_id()); //部门的department_id就是user_id
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onEventMainThread(HttpCallbackEvent event) {

        if (KeyConstants.URL_GET_GOV_GRID_LEAD.equals(event.getTag())) {

            if (HandlerCallback.HttpFlow.START == event.getHttpFlow()) {
                mHUDUtil.show();
            } else if (HandlerCallback.HttpFlow.FINISH == event.getHttpFlow()) {
                mHUDUtil.hidden();

                if (Constants.Result.SUCCESS == event.getEventResult()) {

                    if (mAdapter != null) {
                        mDataList.clear();
                        mAdapter.notifyDataSetChanged();
                        mAdapter.addItems((List<PersonVo>) event.getObj());
                    }

                } else if (Constants.Result.ERROR == event.getEventResult()) {
                    mHUDUtil.showErrorTip(event.getMsg(), new HUDUtil.CallBack() {
                        @Override
                        public void onCall() {
                            callFinish();
                        }
                    });
                }
            }

        }

    }


}
